KVM : GPU Passthrough
2018/01/31 |
Configure GPU Passthrough for Virtual Machines.
By this configuration, it's possible to use GPU on Virtual Machines and run
GPU Computing by CUDA,
Machine learning/Deep Learning by TensorFlow.
Before configuration, Enable VT-d (Intel) or AMD IOMMU (AMD) on BIOS Setting first.
|
|
[1] | Enable IOMMU on KVM Host. |
[root@dlp ~]#
vi /etc/default/grub # line 6: add (if AMD CPU, add [amd_iommu=on]) GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet intel_iommu=on "GRUB_DISABLE_RECOVERY="true" grub2-mkconfig -o /boot/grub2/grub.cfg
[root@dlp ~]#
reboot
# after rebooting, verify IOMMU is enabled like follows [root@dlp ~]# |
[2] | Enable vfio-pci kernel module. It's OK to keep common kernel provided by RHEL/CentOS official. |
# OK with common kernel [root@dlp ~]# uname -a Linux dlp.srv.world 3.10.0-693.17.1.el7.x86_64 #1 SMP Thu Jan 25 20:13:58 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux # show PCI identification number and [vendor-ID:device-ID] of Graphic card # PCI number ⇒ it matchs [03:00.*] below, vendor-ID:device-ID ⇒ it matchs [10de:***] below [root@dlp ~]# lspci -nn | grep -i nvidia 03:00.0 VGA compatible controller [0300]: NVIDIA Corporation GP106 [GeForce GTX 1060 6GB] [10de:1c03] (rev a1) 03:00.1 Audio device [0403]: NVIDIA Corporation GP106 High Definition Audio Controller [10de:10f1] (rev a1)
[root@dlp ~]#
vi /etc/modprobe.d/vfio.conf # create new: for [ids=***], specify [vendor-ID:device-ID] options vfio-pci ids=10de:1c03,10de:10f1 echo 'vfio-pci' > /etc/modules-load.d/vfio-pci.conf
[root@dlp ~]#
reboot
# it's OK if it's enabled like follows [root@dlp ~]# dmesg | grep -i vfio [ 4.338817] VFIO - User Level meta-driver version: 0.3 [ 4.348806] vfio_pci: add [10de:1c03[ffff:ffff]] class 0x000000/00000000 [ 4.348958] vfio_pci: add [10de:10f1[ffff:ffff]] class 0x000000/00000000 |
[3] | Update QEMU. |
[root@dlp ~]#
/usr/libexec/qemu-kvm -version QEMU emulator version 1.5.3 (qemu-kvm-1.5.3-141.el7_4.6), Copyright (c) 2003-2008 Fabrice Bellard
[root@dlp ~]#
yum -y install centos-release-qemu-ev # disable usually [root@dlp ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/CentOS-QEMU-EV.repo
# for this installing, [qemu-kvm] package is replaced to [qemu-kvm-ev] package [root@dlp ~]# yum --enablerepo=centos-qemu-ev -y install qemu-kvm-ev
[root@dlp ~]#
systemctl restart libvirtd
[root@dlp ~]#
/usr/libexec/qemu-kvm -version QEMU emulator version 2.9.0(qemu-kvm-ev-2.9.0-16.el7_4.13.1) Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers # verify q35 is included like follows [root@dlp ~]# /usr/libexec/qemu-kvm -machine help Supported machines are: pc RHEL 7.4.0 PC (i440FX + PIIX, 1996) (alias of pc-i440fx-rhel7.4.0) pc-i440fx-rhel7.4.0 RHEL 7.4.0 PC (i440FX + PIIX, 1996) (default) pc-i440fx-rhel7.3.0 RHEL 7.3.0 PC (i440FX + PIIX, 1996) pc-i440fx-rhel7.2.0 RHEL 7.2.0 PC (i440FX + PIIX, 1996) pc-i440fx-rhel7.1.0 RHEL 7.1.0 PC (i440FX + PIIX, 1996) pc-i440fx-rhel7.0.0 RHEL 7.0.0 PC (i440FX + PIIX, 1996) rhel6.6.0 RHEL 6.6.0 PC rhel6.5.0 RHEL 6.5.0 PC rhel6.4.0 RHEL 6.4.0 PC rhel6.3.0 RHEL 6.3.0 PC rhel6.2.0 RHEL 6.2.0 PC rhel6.1.0 RHEL 6.1.0 PC rhel6.0.0 RHEL 6.0.0 PC q35 RHEL-7.4.0 PC (Q35 + ICH9, 2009) (alias of pc-q35-rhel7.4.0) pc-q35-rhel7.4.0 RHEL-7.4.0 PC (Q35 + ICH9, 2009) pc-q35-rhel7.3.0 RHEL-7.3.0 PC (Q35 + ICH9, 2009) none empty machine |
[4] | It's OK all. Create a new VM with GPU. For [--host-device], specify GPU, and for [--machine], specify [q35], for [--features], specify [kvm_hidden=on]. |
[root@dlp ~]# virt-install \
--name centos7 \ --ram 8192 \ --disk path=/var/kvm/images/centos7.img,size=30 \ --vcpus 4 \ --os-type linux \ --os-variant rhel7 \ --network bridge=br0 \ --graphics none \ --console pty,target_type=serial \ --location 'http://ftp.iij.ad.jp/pub/linux/centos/7/os/x86_64/' \ --extra-args 'console=ttyS0,115200n8 serial' \ --host-device 03:00.0 \ --features kvm_hidden=on \ --machine q35 |
[5] | After finishing create VM, verify Graphic card is shown on VM. If no problem, try to Install Graphic Driver, GPU Computing by CUDA, Machine learning/Deep Learning by TensorFlow. |
[root@localhost ~]# lspci | grep -i nvidia 04:00.0 VGA compatible controller: NVIDIA Corporation GP106 [GeForce GTX 1060 6GB] (rev a1) |